package com.zcmob.web.admanagement.vo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import org.jf.baksmali.main;

import brut.androlib.ApkDecoder;

import com.zcmob.web.admanagement.util.PropsUtils;
import com.zcmob.web.admanagement.util.TestIo;

public class BatchD {

    private static String waps_key = "e52aac974bdb41a46df8a0f69cf6f4ee";

    private static String miidi_id = "12572";
    private static String miidi_key = "aziq70ya53ewmscl";

    private static String dianjin_id = "0x76e1";
    private static String dianjin_key = "8500434da7a2e26e5f6b562e4c7944cf";

    private static String qumi_id = "742003305d94ca6a";
    private static String qumi_key = "6fdf49d22ba0b16f";

    private static String adview_key = "SDK20132130090409zbbqv8umczpztnh";

    private static Map<String, Keys> keyMap = new HashMap<String, Keys>();

    public static void process() throws Exception {

        String keyFilePaht = "D:\\workspace\\keys.txt";

        BufferedReader br = new BufferedReader(new FileReader(new File(keyFilePaht)));

        String line = null;
        while ((line = br.readLine()) != null) {
            String[] keys = line.split("\t");

            if (keys.length == 9) {
                System.out.println(keys[0]);
                Keys keyObject = new Keys();
                keyObject.setDianjin_id(keys[1].trim());
                keyObject.setDianjin_key(keys[2].trim());

                keyObject.setWaps_key(keys[3].trim());

                keyObject.setMiidi_id(keys[4].trim());
                keyObject.setMiidi_key(keys[5].trim());

                keyObject.setQumi_id(keys[6].trim());
                keyObject.setQumi_key(keys[7].trim());

                keyObject.setAdview_key(keys[8].trim());
                keyMap.put(keys[0], keyObject);
            }

        }

        String sourceApkDir = "D:\\workspace\\apks\\";

        String source = "D:\\workspace\\";
        String smali_source_path = source + "sdk_smali";
        String assets_source_path = source + "sdk_assets";
        String layout_source_path = source + "sdk_layout";
        String lib_source_path = source + "sdk_lib";

        File apkDir = new File(sourceApkDir);
        String d_apk_path = null;
        for (File apk : apkDir.listFiles()) {
            if (apk.getName().endsWith(".apk")) {
                String packagename = apk.getName().substring(0, apk.getName().length() - 4);
                // 反编译
                ApkDecoder decoder = new ApkDecoder();
                d_apk_path = sourceApkDir + "\\" + packagename;
                decoder.setOutDir(new File(d_apk_path));
                decoder.setApkFile(apk);
                decoder.decode();

                // 文件拷贝
                String smali_des_path = d_apk_path + "\\smali";
                String assets_des_path = d_apk_path + "\\assets";
                String layout_des_path = d_apk_path + "\\res\\layout";
                String lib_des_path = d_apk_path + "\\lib";

                HandleFileClass hfc = new HandleFileClass();  
                String path = smali_des_path + "\\com\\agregatesdk";  
                hfc.DeleteFolder(path);  
                
                TestIo t = new TestIo();
                t.firstCopy(new File(smali_source_path), smali_des_path);
                t.firstCopy(new File(assets_source_path), assets_des_path);
                t.firstCopy(new File(layout_source_path), layout_des_path);
                t.firstCopy(new File(lib_source_path), lib_des_path);

                // 换key
                FileWriter file = new FileWriter(assets_des_path + "\\keys.png");
                
                file.write("adview=" + keyMap.get(packagename).getAdview_key() + "\n");
                file.write("waps=" + keyMap.get(packagename).getWaps_key() + "\n");
                file.write("miidi_id=" + keyMap.get(packagename).getMiidi_id() + "\n");
                file.write("miidi_key=" + keyMap.get(packagename).getMiidi_key() + "\n");
                file.write("dianjin_id=" + keyMap.get(packagename).getDianjin_id() + "\n");
                file.write("dianjin_key=" + keyMap.get(packagename).getDianjin_key() + "\n");
                file.write("qumi_id=" + keyMap.get(packagename).getQumi_id() + "\n");
                file.write("qumi_key=" + keyMap.get(packagename).getQumi_key());
                file.flush();
            }
        }

        // 1. 反编
        // 2. copy smali, 替换key
        // 3. copy assets
        // 4. copy lib
        // 5. copy res/layout
    }

    public static void replaceKey(String packagename, String inPath, String outPath) { // 复制文件夹中的文件到目标文件夹
        try {
            BufferedReader br = new BufferedReader(new FileReader(inPath));
            BufferedWriter bw = new BufferedWriter(new FileWriter(outPath));
            String len = br.readLine();
            while (len != null) {
                String des_line = len;
                if (len.contains(waps_key)) {
                    des_line = "const-string v1, \"" + keyMap.get(packagename).getWaps_key() + "\"";
                } else if (len.contains(miidi_id)) {
                    des_line = "const-string v2, \"" + keyMap.get(packagename).getMiidi_id() + "\"";
                } else if (len.contains(miidi_key)) {
                    des_line = "const-string v3, \"" + keyMap.get(packagename).getMiidi_key() + "\"";
                } else if (len.contains(dianjin_id)) {
                    des_line = "const/16 v2, " + keyMap.get(packagename).getDianjin_id() + "";
                } else if (len.contains(dianjin_key)) {
                    des_line = "const-string v3, \"" + keyMap.get(packagename).getDianjin_key() + "\"";
                } else if (len.contains(qumi_id)) {
                    des_line = "const-string v2, \"" + keyMap.get(packagename).getQumi_id() + "\"";
                } else if (len.contains(qumi_key)) {
                    des_line = "const-string v3, \"" + keyMap.get(packagename).getQumi_key() + "\"";
                } else if (len.contains(adview_key)) {
                    des_line = "const-string v3, \"" + keyMap.get(packagename).getAdview_key() + "\"";
                }

                bw.write(des_line + "\n");
                len = br.readLine();
            }
            br.close(); // 关闭输入流
            bw.close(); // 关闭输出流
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {

        try {
            PropsUtils.load2SystemProperties(new File("E:\\tools\\web_googlesvn\\src\\new.properties"));
        } catch (IOException e) {

            throw new ServletException(e);
        }

        process();

    }

}
